package com.example.huangliting.experimentalarrangementsystem.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huangliting.experimentalarrangementsystem.R;
import com.example.huangliting.experimentalarrangementsystem.activity.AddLessonActivity;
import com.example.huangliting.experimentalarrangementsystem.activity.LoginActivity;
import com.example.huangliting.experimentalarrangementsystem.activity.MyScheduleDetailAct;
import com.example.huangliting.experimentalarrangementsystem.activity.MyScheduleViewAct;
import com.example.huangliting.experimentalarrangementsystem.activity.PersonalMsgModifyAct;
import com.example.huangliting.experimentalarrangementsystem.bean.ClassInfo;
import com.example.huangliting.experimentalarrangementsystem.util.DialogButtonListener;
import com.example.huangliting.experimentalarrangementsystem.util.DialogUtil;
import com.leon.lib.settingview.LSettingItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangLiting on 2018/3/19.
 */

public class MineFragment extends Fragment {

    public MineFragment() {
        // Required empty public constructor
    }

    private Button logoutBtn;
    private SharedPreferences sp;
    private TextView tv_title_img;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //对一个控件进行点击事件
        final View view = inflater.inflate(R.layout.fragment_mine, container, false);
        LSettingItem one =(LSettingItem)view.findViewById(R.id.item_one);
        LSettingItem lid_myschedule =  (LSettingItem) view.findViewById(R.id.my_schedule_lid);
        LSettingItem aboutApp_lsi =  (LSettingItem) view.findViewById(R.id.about_app_lsi);
        LinearLayout personImg_ll = view.findViewById(R.id.personImg_ll);
        logoutBtn = view.findViewById(R.id.logout_btn);
        tv_title_img = view.findViewById(R.id.img_title);
        sp = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        if (sp.getBoolean("isLogin",false)){
            logoutBtn.setVisibility(View.VISIBLE);
            tv_title_img.setText(sp.getString("userId",""));

        }

        //我的头像登录
        personImg_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(i, 1);
            }
        });

        //我的资料
        one.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                if(sp.getBoolean("isLogin",false)){
                    Intent i = new Intent(getActivity(), PersonalMsgModifyAct.class);
                    startActivity(i);
                }else
                    Toast.makeText(view.getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
            }
        });

        //我的课程
        lid_myschedule.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                if(sp.getBoolean("isLogin",false)){
                    Intent i = new Intent(getActivity(), MyScheduleViewAct.class);
                    startActivity(i);
                }else
                    Toast.makeText(view.getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
            }
        });

        //关于应用
        aboutApp_lsi.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                String[] items = {"名称：实验排课与发布系统","版本：Android1.0"};
                showListAlertDialog(view,items);
            }
        });

        logoutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil dialogUtil = new DialogUtil();
                dialogUtil.show(v.getContext(), "确定退出登录吗?", new DialogButtonListener() {
                    @Override
                    public void sure() {
                        Toast.makeText(view.getContext(),"注销成功",Toast.LENGTH_SHORT).show();
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("isLogin",false);
                        editor.putBoolean("auto_ischeck",false);
                        editor.commit();
                        logoutBtn.setVisibility(View.GONE);
                        tv_title_img.setText("未登录，请点击登录");
                    }

                    @Override
                    public void cancel() {
                        //Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (1 == requestCode) {
            if (sp.getBoolean("isLogin",false)){
                logoutBtn.setVisibility(View.VISIBLE);
                tv_title_img.setText(sp.getString("userId",""));
            }
        }
    }

    // 信息列表提示框
    private AlertDialog alertDialog;
    public void showListAlertDialog(final View view, final String[] items){

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(view.getContext());
        alertBuilder.setTitle("关于应用");
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int index) {
            }
        });
        alertDialog = alertBuilder.create();
        alertDialog.show();
    }
}
